package com.hncc.kumao.util;

import java.util.Date;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class JWTUtils {

    //创建JWT
    public static String createJWT(String id) {
        //long now = System.currentTimeMillis();//当前时间 
        //long exp = now + 1000*60;//过期时间为1分钟
        JwtBuilder builder= Jwts.builder()
            .setId(id)
            .setSubject("这是" + id + "的token")
            .setIssuedAt(new Date())
            .signWith(SignatureAlgorithm.HS256,ConstValue.JWT_KEY);
            /*.setExpiration(new Date(exp));*///设置过期时间
        return builder.compact();
    }
    
    //解析JWT
    public static String parseJWT(String jwt) {
        Claims claims = Jwts.parser().setSigningKey(ConstValue.JWT_KEY).parseClaimsJws(jwt).getBody();
        return claims.getId();
    }
}
